package com.wn.controller;

import com.wn.annotation.RequireRoles;
import com.wn.entity.Emp;
import com.wn.entity.PageParam;
import com.wn.entity.Role;
import com.wn.entity.Userinfo;
import com.wn.service.UserinfoService;
import com.wn.service.impl.UserinfoServiceImpl;
import com.wn.utils.JwtUtil;
import com.wn.utils.Rsp;
import com.wn.utils.SystemControllerLog;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * (Userinfo)表控制层
 *
 * @author makejava
 * @since 2020-10-26 09:43:15
 */
@RestController
@RequestMapping("user")
public class UserinfoController {

//    private static final Logger log = LoggerFactory.getLogger(UserinfoController.class);
    /**
     * 服务对象
     */
    @Resource
    private UserinfoService userinfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Userinfo selectOne(String id) {
        return this.userinfoService.queryById(id);
    }

    @SystemControllerLog(description = "分页查询用户信息")
    @PostMapping("/searchSomeNewsUserByPage")
    @RequireRoles(roles = {"admin","super_admin","reception","guide","tourist"},index = 1)
    @ApiOperation("查询所有 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<Userinfo> orderPageParam,@RequestHeader("token") String token) {

        return Rsp.success(userinfoService.searchSomeByPage(orderPageParam));
    }

    @SystemControllerLog(description = "动态修改用户信息")
    @RequireRoles(roles = {"admin","super_admin","reception","guide","tourist"},index = 1)
    @PostMapping("/updateUserInfo")
    @ApiOperation("动态修改")
    public Rsp updateNewscategory(@RequestBody Userinfo newsorder,@RequestHeader("token") String token){

        return userinfoService.updateId(newsorder);
    }


    @PostMapping("/addOneUserInfo")
    @ApiOperation("注册")
    public Rsp addOne(@RequestBody @Valid Userinfo userinfo){

        userinfo.setRoleid(4);
        return userinfoService.addOne(userinfo);
    }

    //管理员根据id删除员工信息
    @SystemControllerLog(description = "通过ID物理删除用户信息")
    @RequireRoles(roles = {"super_admin","news_admin"},index = 2)
    @PostMapping("/deleteOneNewsUserTrue")
    @ApiOperation("通过ID物理删除")
    public Rsp deleteOneNewscategoryTrue(@RequestBody Emp newsorder,@RequestHeader("token") String token){

        return userinfoService.deleteOneTrue(newsorder.getId());
    }


    /**
     * 登录
     * @param loginUserinfo
     * @return
     * @throws Exception
     */
    @PostMapping("/loginWithUsernameAndPass")
    public Rsp userinfoLogin(@RequestBody Userinfo loginUserinfo){

        System.out.println(loginUserinfo);
        return userinfoService.userinfoLogin(loginUserinfo);

    }

    /**
     * 通过主键查询单条数据
     *
     * @param token 令牌
     * @return 单条数据
     */
    @GetMapping("getUserInfo")
    public Rsp getUserInfo(@RequestHeader String token){

        String id = JwtUtil.parseToken(token).get("id").asString();
        String roleSign = JwtUtil.parseToken(token).get("roleSign").asString();
        String account = JwtUtil.parseToken(token).get("account").asString();
        String image = JwtUtil.parseToken(token).get("image").asString();

        Userinfo userinfo = new Userinfo();
        userinfo.setId(id);
        userinfo.setRoleSign(roleSign);
        userinfo.setAccount(account);
        userinfo.setCardimage(image);

        return Rsp.success(userinfo);
    }

    /**
     * 注销
     * @return
     */
    @PostMapping("/logout")
    public Rsp logout(){

        return Rsp.success("注销成功");
    }

    /**
     * 根据电话修改密码
     * @param
     * @return
     */
    @PostMapping("/selphone")
    public Rsp selphone(@RequestBody Userinfo userinfo){


        return userinfoService.selphone(userinfo.getPhone(),userinfo.getPassword());
    }

    @PostMapping("/forgetPassword")
    public Rsp forgetPassword(@RequestBody Userinfo userinfo){

        return userinfoService.forgetPassword(userinfo);
    }

}
